package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Car;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICarService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("car")
public class CarController {

    @Autowired
    private ICarService carService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"car:view","车辆信息页面"},logical = Logical.OR)
    public String view() throws Exception{

        return "car/car";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Car> selectAll() throws Exception{
        return carService.selectAll();
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"car:saveOrUpdate","车辆信息新增/修改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Car entity,String brnadname,Long ids){
        JsonResult jsonResult = new JsonResult();
        try {
            if (entity.getId()!=null){
                carService.updateByPrimaryKey(entity,brnadname,ids);
            }else {
                carService.insert(entity);
            }
        }catch (Exception e) {
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"car:delete","车辆信息删除"},logical = Logical.OR)
    public JsonResult delete(Long id) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            carService.deleteByPrimaryKey(id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }



    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo) throws Exception{
        return carService.query(qo);
    }

}
